package com.mysecurity.pojo;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Jane
 * @date 2024-06-28 17:29
 */

@Data
public class SecurityUser implements UserDetails {

    private CrmUser crmUser ;

    private List<String> perssmions; //数据库的权限

    @JSONField(serialize = false)
    private List<GrantedAuthority> authorities =new ArrayList<>();//中转

    public SecurityUser(CrmUser crmUser,List<String> perssmions) {
        this.crmUser =crmUser;
        this.perssmions =perssmions;
    }
/*   public SecurityUser(CrmUser crmUser,List<GrantedAuthority> perssmions) {
       this.crmUser =crmUser;
       this.authorities = perssmions;
   }*/
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() { //spring 承认的权限列表
        if (null!=perssmions){
             //循环遍历 把db里面查出来的权限,放到 Collection<GrantedAuthority>里面来
           /* GrantedAuthority g=null;

            for (String perssion: perssmions ) {

                g= new SimpleGrantedAuthority(perssion);

                authorities.add(g);
            }*/
            authorities = perssmions.stream().map(n -> new SimpleGrantedAuthority(n)).collect(Collectors.toList());
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return crmUser.getUsrPassword();
    }

    @Override
    public String getUsername() {
        return crmUser.getUsrName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
